/* Copyright 2008 Bas van den Berg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef PLAYERSESSION_H
#define PLAYERSESSION_H

#include "Session.h"
#include "AliasList.h"
#include "StringBuilder.h"
#include "PromptBuilder.h"
#include "PlayerSettings.h"

namespace Chronos {

class Connection;
class Parser;
class MudPlayer;
class Database;

class PlayerSession : public Session {
public:
    PlayerSession(const std::string& n, Connection* con, Parser& parser_, Database& database_);
    virtual ~PlayerSession();
    void changeConnection(Connection* con);
    enum SessionResult { OK, FINISHED };
    int handlePulse();

    virtual StringBuilder& write();
    void flush();

    virtual void setLogout() { logout = true; }
    virtual const std::string& getName() const { return name; }
    virtual const std::string& getStatus() const {
        const static std::string statusStr[3] = {"", "(idle)", "(DC)"};
        return statusStr[state];
    }
	virtual AliasList& getAliases() const { return aliases; }
    virtual PlayerSettings& getSettings() const { return settings; } 
    virtual MudPlayer& getPlayer() const { return player; }
    virtual void setPrompt(const std::string& p);
    void save();
    virtual void changePassword(const std::string& newPass);
    virtual unsigned int getPlayTime() const;
    virtual unsigned int getTotalTime() const;
private:
    void handleInput();
    void initAliases();
    void initSettings();
    void checkAutoSave();

    std::string name;
    enum SessionState {ACTIVE, IDLE, DISCONNECTED};
    SessionState state;
    Connection* connection;
    Parser& parser;
    Database& database;
    unsigned int timeout;
    unsigned int autoSaveInterval;
    bool logout;

    static const unsigned int MAX_INPUT_LENGTH = 63;
    char readBuffer[MAX_INPUT_LENGTH * 2];		// longer for aliases
    StringBuilder buffer;
    std::string lastCommand;

    MudPlayer& player;
	bool addPrompt;
    PromptBuilder prompt;

	mutable AliasList aliases;
    mutable PlayerSettings settings;

    const unsigned int loginTime;
    const unsigned int totalTime;

    PlayerSession(const PlayerSession&);
    PlayerSession& operator=(const PlayerSession&);
};

}

#endif

